package Data_Structure.Sort;

import java.util.Arrays;

/**
 * @className: CountSort
 * @author: 芃
 * @description: 计数排序-》作为了解
 * @date: 2023/8/27 17:45
 */
public class CountSort {
    public static void countSort(int[] array){
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max){
                max = array[i];
            }
            if (array[i] < min){
                min = array[i];
            }
        }
        int capacity = max - min + 1;
        int[] arr = new int[capacity];
        for (int i = 0; i < array.length; i++) {
            int index = array[i] - min;
            arr[index]++;
        }
        int[] ret = new int[array.length];
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            int time = arr[i];
            for (int j = 0; j < time; j++) {
                ret[index++] = i + min;
            }
        }
        System.out.println(Arrays.toString(ret));
        for (int i = 0; i < array.length; i++) {
            array[i] = ret[i];
        }
    }
}
